<template>
  <div v-infinite-scroll="load" :infinite-scroll-disabled="!moreAlbums">
    <el-affix :offset="0">
      <div class="top">
        <BackTo class="backto" :size="30" />
        <span class="tulin tulin-Plus" @click="dialogTableVisible1=!dialogTableVisible1"></span>
        <div class="col-width"></div>
      </div>
    </el-affix>
    <!-- <el-affix :offset="0">
      <div class="top">
        <el-dropdown trigger="click" size="large">
          <el-icon :size="30">
            <plus />
          </el-icon>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="dialogTableVisible1=!dialogTableVisible1">共享链接
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
        &nbsp;
        <el-dropdown trigger="click" size="large">
          <el-icon :size="30">
            <setting />
          </el-icon>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>设置</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-affix> -->
    <!-- -------------------相册集合----------------------------- -->
    <div class="content">
      <el-row :gutter="0">
        <el-col :xs="12" :sm="6" :md="6" :lg="4" :xl="3" v-for="album in albumList" :key="album">
          <el-card class="card" :body-style="{ padding: '0px' }" @click="showPhoto(album.id)">
            <el-image :src="album.image" class="image" fit="cover">
              <template #error>
                <div class="el-image-error">
                  快添加图片吧
                </div>
              </template>
            </el-image>
            <div style="padding: 10px">
              <span>{{album.name}}</span>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
    <!-- 添加共享相册 -->
    <el-dialog v-model="dialogTableVisible1" title="添加共享相册" :center="true" :show-close="false">
      <el-input type="text" v-model="shareLink" placeholder="共享链接" size="large" maxlength="255" />
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogTableVisible1 = !dialogTableVisible1">取消</el-button>
          <el-button type="primary" @click="toAddShareUser">查看</el-button>
        </span>
      </template>
    </el-dialog>
    <div v-loading="loading"></div>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus';
export default {
  name: 'GAlbumShare',
  data () {
    return {
      moreAlbums: true,
      dialogTableVisible1: false,
      albumList: [],
      pageNum: 1,
      loading: false,
      shareLink: "",
    }
  },
  mounted () {
    this.getAlbums(this.pageNum++, 5);
  },
  methods: {
    //添加共享相册
    toAddShareUser () {
      if (this.shareLink == null || "" == this.shareLink.trim()) {
        ElMessage({
          message: "请输入正确链接",
          grouping: true,
          type: 'error',
          duration: 1000,
        })
        return;
      }
      let index = this.shareLink.lastIndexOf("/");
      let albumId = this.shareLink.substring(index + 1);
      if (albumId.length == 19) {
        this.$router.push("/addshareuser/" + albumId);
      } else {
        ElMessage({
          message: "请输入正确链接",
          grouping: true,
          type: 'error',
          duration: 1000,
        })
      }
    },
    //获取相册
    getAlbums (pageNum, pageSize) {
      this.loading = true;
      this.$axios.get("/album/share/" + pageNum + "/" + pageSize + "").then((res) => {
        if (res.flag) {
          console.log("AlbumShare.vue->getAlbums", res);
          if (res.data.length < 1) {
            this.moreAlbums = false
          } else {
            for (var key in res.data) {
              this.albumList.push(res.data[key]);
            }
          }
        }
        this.loading = false;
      })
    },
    load () {
      this.getAlbums(this.pageNum++, 5);
    },
    showPhoto (albumId) {
      console.log(albumId);
      this.$router.push({
        name: 'photoshare', params: { "albumId": albumId }
      }
      );
    }
  }
}
</script>

<style scoped>
.top {
  width: 100%;
  text-align: right;
  background-color: var(--primary);
}
.backto {
  position: absolute;
  left: 5px;
}
.card {
  margin-top: 5px;
  padding: 5px;
  text-align: center;
}
/* 解决getter 超出出现滚动条 */
.content {
  overflow-x: hidden;
  padding: 0 5px;
  box-sizing: border-box;
}
.image {
  height: 150px;
  width: 100%;
}
.el-image-error {
  height: 100%;
  line-height: 150px;
  color: gray;
}
</style>